<?php 
require('Common/ConnDB.php');
require('Model.php');
class UserModel extends Model
{	
	public function __construct()
	{
		parent::__construct();

	}
	public function getUserList()
	{
		$sql="select * from users";
		$data=$this->conn->query($sql);
		return $data;
	}
	//将用户信息写入数据库
	public function addUserInfo()
	{	//获取用户提交的内容
		$username = trim($_POST['username']);
        $password = $_POST['password'];
        $gender = $_POST['gender'];
        $birthdate = $_POST['birthdate'];
        $hobby = implode($_POST['hobby'], '，'); //将数组转换成字符串
        $degree = $_POST['degree'];
        $intro = $_POST['intro'];
       
        	$pic=$this->uploadfile("pic",'images/');
        	$sql="insert into users values(null,'$username',md5('$password'),'$gender','$birthdate','$hobby','$degree','$intro','$pic')";
			$result=$this->conn->query($sql);//往数据库写入信息
			return $result;
     
  
    }
          //更新用户，获取用户信息
        public function getUserbyID()
        {
        	$uid=$_GET['uid'];
        	$sql="select *from users where uid=$uid";
        	$data=$this->conn->query($sql);
            return $data;
        }
         public function updateUserInfo(){
      //上传头像文件 
     $pic=$this->uploadfile("pic",'images/');
     //1.获取修改用户提交的内容
     $uid=$_POST['uid'];
  $uname=$_POST['uname'];
  $password=$_POST['password'];
  $gender=$_POST['gender'];
  $birthdate=$_POST['birthdate'];
  $hobby=implode(",", $_POST['hobby']);
  $degree=$_POST['degree'];
  $intro=$_POST['intro'];
  if($_FILSES['pic']['filename']=='')
  {

   $sql="update users set uname='{$uname}',password=md5('{$password}'),gender='{$gender}',birthdate='{$birthdate}',hobby='{$hobby}',degree='{$degree}',intro='{$intro}',pic='{$pic}' where uid=$uid";
  }
  else
  {

   $sql="update users set uid='null',uname='{$uname}',password=md5('{$password}'),gender='{$gender}',birthdate='{$birthdate}',hobby='{$hobby}',degree='{$degree}',intro='{$intro}',pic='{$pic}' where uid=$uid";
     }
   $result=$this->conn->query($sql);//写入数据库
   return $result;
    }
     public function delUserbyID()
     {
        $uid=$_GET['uid'];
          $sql="delete from users where uid=$uid";
          $data=$this->conn->query($sql);
            return $data;
     }

}

 ?>
